function varargout = aarde(varargin)
% AARDE MATLAB code for aarde.fig
%      AARDE, by itself, creates a new AARDE or raises the existing
%      singleton*.
%
%      H = AARDE returns the handle to a new AARDE or the handle to
%      the existing singleton*.
%
%      AARDE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in AARDE.M with the given input arguments.
%
%      AARDE('Property','Value',...) creates a new AARDE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before aarde_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to aarde_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help aarde

% Last Modified by GUIDE v2.5 27-Apr-2012 17:18:42

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @aarde_OpeningFcn, ...
                   'gui_OutputFcn',  @aarde_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before aarde is made visible.
function aarde_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to aarde (see VARARGIN)

% Choose default command line output for aarde
handles.output = hObject;

% Initialisatie
fprintf('Enter COM port: ');
COM = sscanf(input('','s'),'%s');
handles.enable = true;

copyfile('data.txt','data_backup.txt');

handles.file = fopen('data.txt','w');
if(~isequal(COM,[]))
    handles.s = serialInit(COM);
else
    handles.s = fopen('Afstanden.data','r');
end

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes aarde wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = aarde_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


hold on
global number
global points
% Main loop
while(handles.enable)
    pause(0.01);
    
    if (~ishandle(hObject) || ~handles.enable)
        handles.enable = false;
      
        %if(~isequal(handles.s,[]))
        %    handles.s = fclose('all');
        %end
        %if(~isequal(handles.file,[])||~isequal(handles.file,0))
        %    handles.file = fclose(handles.file);
        %end
        %hold off
        %delete(handles.figure1)
        return;
    end 
      
    data = serialData(handles.s);
    if(isequal(data,[]))
        continue;
    end
    mainLoop(data, handles.file);
    set(handles.number, 'String', sprintf('number of cars:\n%i', number));
    set(handles.points, 'String', sprintf('number of points:\n%i', points));
    
    % Update handles structure
    guidata(hObject, handles);
end


% --- Executes on button press in exit_pushbutton.
function exit_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to exit_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Get the current position of the GUI from the handles structure
% to pass to the modal dialog.
pos_size = get(handles.figure1,'Position');
% Call modaldlg with the argument 'Position'.
user_response = exit_confirm('Title','Confirm Close');
switch user_response
case {'No'}
	% take no action
case 'Yes'
    % close
    handles.enable = false;
    % Update handles structure
    guidata(hObject, handles);
    
    if(~isequal(handles.s,[]))
        handles.s = fclose('all');
    end
    %if(~isequal(handles.file,[]))
    %    handles.file = fclose(handles.file);
    %end
    %hold off
	delete(handles.figure1)
end
